package io.sage.modules.basics.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.sage.modules.basics.entity.BGoodsArea;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.common.utils.R;
import io.sage.modules.basics.entity.BGoodsShelves;
import io.sage.modules.basics.service.BGoodsShelvesService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;



/**
 * 货架管理
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-07-08 22:38:04
 */
@RestController
@RequestMapping("basics/bgoodsshelves")
public class BGoodsShelvesController {
    @Autowired
    private BGoodsShelvesService bGoodsShelvesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("basics:bgoodsshelves:list")
    public R list(@RequestParam Map<String, Object> params){
    	Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		if(params.get("shelveName")!=null){

			if(!"".equals(params.get("shelveName").toString())){
				paramMap.put("shelveName", params.get("shelveName").toString());
			}
		}

		if(params.get("shelveCode")!=null){
			if(!"".equals(params.get("shelveCode").toString())){
				paramMap.put("shelveCode", params.get("shelveCode").toString());
			}
		}

		if(params.get("goodsAreaId")!=null){
			if(!"".equals(params.get("goodsAreaId").toString())){
				paramMap.put("goodsAreaId", params.get("goodsAreaId").toString());
			}
		}
		if(params.get("wareHouseId")!=null){
			if(!"".equals(params.get("wareHouseId").toString())){
				paramMap.put("wareHouseId", params.get("wareHouseId").toString());
			}
		}
		paramMap.put("orderby", "gs.update_time desc,gs.create_time desc");
		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BGoodsShelves> selectGoodsShelvesList = bGoodsShelvesService.selectGoodsShelvesList(paramMap);

		PageInfo<BGoodsShelves> pageInfo = new PageInfo<BGoodsShelves>(selectGoodsShelvesList);// 传入list就可以了

		return R.ok().put("goodsShelvesList", pageInfo);
    }
	/**
	 * 修改
	 */
	@RequestMapping("/getAll")
	public R getAll(@RequestParam Map<String, Object> params){
		params.put("orderby", "gs.shelve_code");
		List<BGoodsShelves> selectGoodsAreaList = this.bGoodsShelvesService.selectGoodsShelvesList(params);
		return R.ok().put("list", selectGoodsAreaList);
	}

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("basics:bgoodsshelves:info")
    public R info(@PathVariable("id") Integer id){
    	BGoodsShelves goodsShelves = bGoodsShelvesService.selectByPrimaryKey(id);
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("goodsShelves", goodsShelves);

		return R.ok().put("map", paramMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("basics:bgoodsshelves:save")
    public R save(@RequestBody JSONObject jsonObject){
    	BGoodsShelves goodsShelves = jsonObject.getObject("goodsShelves", BGoodsShelves.class);

		SysUserEntity user = ShiroUtils.getUserEntity();
		goodsShelves.setCreateUserId(user.getUserId().intValue());
		int insert = bGoodsShelvesService.insert(goodsShelves);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("basics:bgoodsshelves:update")
    public R update(@RequestBody JSONObject jsonObject){
    	BGoodsShelves goodsShelves = jsonObject.getObject("goodsShelves", BGoodsShelves.class);
		
		if(goodsShelves.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = bGoodsShelvesService.updateByPrimaryKeySelective(goodsShelves);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("basics:bgoodsshelves:delete")
    public R delete(@RequestBody Integer[] ids){
    	
    	int deleteBatchIds = bGoodsShelvesService.deleteBatchIds(ids);
    	if(deleteBatchIds>0){
			return R.ok();
		}
        return R.error();
    }

}
